﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WISD.CarFriend.BLL.OA;

namespace WISD.CarFriend.Web.Admin.OA.EmployeeManage
{
    public partial class EmployeeEdit : Utility.BasicPage
    {
        #region 字段
        private EmployeeModule m_EmployeeModuleBLL = new EmployeeModule();
        private ShopModule m_ShopModuleBLL = new ShopModule();
        private ParaItemModule m_ParaItemModuleBLL = new ParaItemModule();
        private string m_EmployeeId;
        private string m_EmployeeImage;
        private string m_IdentityCardImage;
        #endregion


        #region 属性
        /// <summary>
        /// 人员照片
        /// </summary>
        public string EmployeeImage
        {
            get { return m_EmployeeImage; }
            set { m_EmployeeImage = value; }
        }


        /// <summary>
        /// 身份证图片
        /// </summary>
        public string IdentityCardImage
        {
            get { return m_IdentityCardImage; }
            set { m_IdentityCardImage = value; }
        }
        #endregion

        #region 事件
        /// <summary>
        /// Page_Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] == null)
            {
                base.ShowMessageBox(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改的信息不存在或参数不正确。", "back", "Error");
                return;
            }
            else
            {
                this.m_EmployeeId = Request.QueryString["id"].ToString().Trim();
            }

            if (!IsPostBack)
            {
                // 验证权限
                base.CheckPermission("EmployeeManage_Edit");

                // 绑定角色下拉框
                ddlCompany.DataSource = this.m_ShopModuleBLL.GetList(" IsDel = 0 ");
                ddlCompany.DataTextField = "ShopName";
                ddlCompany.DataValueField = "Id";
                ddlCompany.DataBind();

                ddlClass.DataSource = this.m_ParaItemModuleBLL.GetList(" ParaTypeId = 1 ");
                ddlClass.DataTextField = "ParaItem";
                ddlClass.DataValueField = "Id";
                ddlClass.DataBind();

                ddlPosition.DataSource = this.m_ParaItemModuleBLL.GetList(" ParaTypeId = 2 ");
                ddlPosition.DataTextField = "ParaItem";
                ddlPosition.DataValueField = "Id";
                ddlPosition.DataBind();

                // 赋值
                WISD.CarFriend.Model.CFModel.T_CF_OA_Employee employeeModel
                    = this.m_EmployeeModuleBLL.GetModel(this.m_EmployeeId);
                if (employeeModel != null)
                {
                    txtEmployeeID.Text = employeeModel.EmployeeID;
                    txtName.Text = employeeModel.Name;
                    rblSex.SelectedValue = employeeModel.Sex.ToString();
                    txtEmployeeImage.Text = employeeModel.EmployeeImage;
                    txtBirthday.Text = employeeModel.Birthday.ToString();
                    txtIdentityCard.Text = employeeModel.IdentityCard;
                    txtIdentityCardImage.Text = employeeModel.IdentityCardImage;
                    ddlClass.SelectedValue = employeeModel.Class.ToString();
                    ddlPosition.Text = employeeModel.Position.ToString();
                    ddlCompany.Text = employeeModel.Company.ToString();
                    rblIsDisabled.SelectedValue = employeeModel.IsDisabled.ToString();
                    txtMobileTel.Text = employeeModel.MobileTel;
                    txtQQ.Text = employeeModel.QQ;
                    txtEmail.Text = employeeModel.Email;
                    txtEducation.Text = employeeModel.Education;
                    txtSchool.Text = employeeModel.School;
                    txtPolitical.Text = employeeModel.Political;
                    txtFamilyAddress.Text = employeeModel.FamilyAddress;
                    txtPresentAddress.Text = employeeModel.PresentAddress;
                    txtHomePhone.Text = employeeModel.HomePhone;
                    fckEducationalBackground.Value = employeeModel.EducationalBackground;
                    fckFamilyMember.Value = employeeModel.FamilyMember;
                    fckWorkExperience.Value = employeeModel.WorkExperience;

                    this.m_EmployeeImage = (employeeModel.EmployeeImage == null || employeeModel.EmployeeImage.Trim() == "") ? "" : "<img src=\"" + employeeModel.EmployeeImage  + "\" />";
                    this.m_IdentityCardImage = (employeeModel.EmployeeImage == null || employeeModel.EmployeeImage.Trim() == "") ? "" : "<img src=\"" + employeeModel.IdentityCardImage + "\" />";
                }
                else
                {
                    base.ShowMessageBox(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改的信息不存在或参数不正确。", "back", "Error");
                }
            }
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                WISD.CarFriend.Model.CFModel.T_CF_OA_Employee employeeModel
                    = this.m_EmployeeModuleBLL.GetModel(this.m_EmployeeId);
                employeeModel.EmployeeID = txtEmployeeID.Text.Trim();
                employeeModel.Name = txtName.Text.Trim();
                employeeModel.Sex = int.Parse(rblSex.SelectedValue);
                employeeModel.EmployeeImage = txtEmployeeImage.Text.Trim();
                if (txtBirthday.Text.Trim() == "")
                {
                    employeeModel.Birthday = null;
                }
                else
                {
                    employeeModel.Birthday = DateTime.Parse(txtBirthday.Text.Trim());
                }
                employeeModel.IdentityCard = txtIdentityCard.Text.Trim();
                employeeModel.IdentityCardImage = txtIdentityCardImage.Text.Trim();
                employeeModel.Class = int.Parse(ddlClass.SelectedValue);
                employeeModel.Position = int.Parse(ddlPosition.Text.Trim());
                employeeModel.Company = int.Parse(ddlCompany.Text.Trim());
                employeeModel.IsDisabled = int.Parse(rblIsDisabled.SelectedValue);
                employeeModel.MobileTel = txtMobileTel.Text.Trim();
                employeeModel.QQ = txtQQ.Text.Trim();
                employeeModel.Email = txtEmail.Text.Trim();
                employeeModel.Reviser = Session["UserCode"].ToString();
                employeeModel.ReviseDate = DateTime.Now;
                employeeModel.Education = txtEducation.Text.Trim();
                employeeModel.School = txtSchool.Text.Trim();
                employeeModel.Political = txtPolitical.Text.Trim();
                employeeModel.FamilyAddress = txtFamilyAddress.Text.Trim();
                employeeModel.PresentAddress = txtPresentAddress.Text.Trim();
                employeeModel.HomePhone = txtHomePhone.Text.Trim();
                employeeModel.EducationalBackground = fckEducationalBackground.Value.Trim();
                employeeModel.FamilyMember = fckFamilyMember.Value.Trim();
                employeeModel.WorkExperience = fckWorkExperience.Value.Trim();
                this.m_EmployeeModuleBLL.Update(employeeModel);
                ShowMessageBox("修改成功！", "EmployeeList.aspx", "Success");
            }
            catch (Exception ex)
            {
                base.ShowMessageBox(350, 230, "错误提示", "<b>出现错误啦！</b>" + ex.Message.ToString(), "back", "Error");
            }
        }
        #endregion
    }
}